"use strict"
var jyyModal = {};
jyyModal.prototype = {modal: null};
jyyModal.mapModal = {};
jyyModal.id = 0;
jyyModal.open = function(url,param,initFun,backFun){
	this.modal = new JYYModal(url,param,initFun,backFun);
	jyyModal.putModal(this.modal);
	return this;
};
jyyModal.waiting = function(param,initFun,backFun){
	if(param==null){
		param = {};
	}
	if(typeof(param["height"])=="undefined"){
		param["height"] = 124;
	}
	if(typeof(param["width"])=="undefined"){
		param["width"] = 124;
	}
	if(typeof(param["msg"])=="undefined"||param["msg"]==null||param["msg"].trim()==""){
		param["msg"] = "正在运行，请稍候！";
	}
	this.modal = new JYYModal(null,param,initFun,backFun);
	var idModal = "modal"+this.modal.id;
	$("#"+idModal).empty();
	$("#"+idModal).attr("title",param["msg"]);
	$("#"+idModal).html("<img src='css/images/wait.gif'>");
	return this;
};
jyyModal.confirm = function(param,backFun,initFun){
	var self = this;
	if(param==null){
		param = {width:400,height:150};
	}
	if(typeof(param["height"])=="undefined"||param["height"]<150){
		param["height"]=150;
	}
	if(typeof(param["title"])=="undefined"){
		param["title"] = "确认";
	}
	this.modal = new JYYModal(null,param,initFun,backFun);
	
	var msg = "";
	if(typeof(param["msg"])!="undefined"){
		msg = param["msg"];
	}else{
		msg = param["title"];
	}
	var html =  "<div id='msg"+this.modal.id+"' style='position:absolute;left:15px;top:15px;width:"+(this.modal.width-15)+"px;height:30px;line-height:30px;'>"+msg+"</div>" +
	        "<hr style='position:absolute;left:0px;top:45px;width:"+this.modal.width+"px';>" +
			"<div id='footer"+this.modal.id+"' style='position:absolute;right:15px;top:80px;'>" +
			"<button type='button' id='confirm"+this.modal.id+"' class='btn btn-primary btn-sm' style='background-color:#21755f;border-color:#195949;'>确定</button>" +
			"&nbsp;&nbsp;&nbsp;&nbsp;"+
			"<button type='button' id='cancel"+this.modal.id+"' class='btn btn-primary btn-sm' style='background-color:#21755f;border-color:#195949;'>取消</button>" +
			"</div>";
	$("#content"+this.modal.id).append(html);
	$("#confirm"+this.modal.id).on("click",function(){
		if(backFun){
			backFun();
		}
		self.close();
	});
	$("#cancel"+this.modal.id).on("click",function(){
		self.close();
	});
	return this;
};

jyyModal.close = function(){
	if(this.modal){
		this.modal.close();
		this.modal = null;
	}
};
jyyModal.closeById = function(id){
	this.modal = jyyModal.mapModal[id];
	this.modal.close();
//	此处需要删除
//	jyyModal.mapModal.remove(jyyModal.mapModal[id]);
}

function JYYModal(url,param,initFun,backFun){
	this.url = url;
	this.initFun = initFun;
	this.backFun = backFun;
	if(param){
		if(typeof(param.width)!="undefined"&&param.width){
			this.width = param.width;
		}
		if(typeof(param.height)!="undefined"&&param.height){
			this.height = param.height;
		}
		if(typeof(param.title)!="undefined"&&param.title){
			this.title = param.title;
		}
		if(typeof(param.resizable)!="undefined"){
			this.resizable = param.resizable;
		}
		if(typeof(param.isCanMin)!="undefined"){
			this.isCanMin = param.isCanMin;
		}
	}else{
		this.width = 600;
		this.height = 400;
		this.resizable = false;
		this.isCanMin = false;
	}
	var w = $(window).width();
	var h = $(window).height();
	this.left = (w-this.width)/2;
	if(this.left<0){
		this.left = 0;
	}
	this.top = (h-this.height)/2;
	if(this.top<0){
		this.top = 0;
	}
	this.id = parseInt(Math.random() * 1000000);
	jyyModal.id = this.id;
	var idMask = "mask"+this.id;//遮罩ID
	var idModal = "modal"+this.id;//模态框ID
	var idTitle = "title"+this.id;//顶部标题栏ID
	var idClose = "close"+this.id;//模态框内容ID
	var idMin = "min"+this.id;//顶部最小化ID
	var idContent = "content"+this.id;//内容ID
	var div = 
        "<div id='"+this.id+"'>"
       +    "<div id='"+idMask+"' style='position:fixed;left:0;width:100%;height:100%;top:0;"
       +          "background:#000000;opacity:0.5;filter:alpha(opacity=50);z-index:10000;'>"
       +    "</div>"
       +    "<div id='msg000000' style='display:none;color:white;'></div>"
       +    "<div id='"+idModal+"' class='tabs10001' style='position:fixed;width:"+this.width+"px;height:"+this.height+"px;top:"+this.top+"px; left:"+this.left+"px;z-index:10001;'>"
       +        "<div id='"+idTitle+"' style='position:absolute;left:0px;width:"+(this.width-30)+"px;height:30px;top:0px; left:0px;'>"
       +            "<div class='modal-title' style='position:absolute;top:0px;left:0px;color:white;line-height:30px;'>"+this.title+"</div>"
       +        "</div>";
       if(this.isCanMin) div += "<div id='"+idMin+"' title='最小化' style='position:absolute;top:0px;right:30px;width:30px;height:30px;line-height:30px;background:url(images/dialog_min.png) #969495;cursor:pointer;'></div>";
       div +=   "<div id='"+idClose+"' title='关闭' style='position:absolute;top:0px;right:0px;width:30px;height:30px;line-height:30px;background:#969495;color:white;padding-left:10px;'>X</div>"
       +        "<div id='"+idContent+"' style='position:absolute;width:"+this.width+"px;height:"+(this.height-30)+"px;top:30px;left:0px;padding:15px;background:white;'>"
       +        "</div>"
       +    "</div>" 
       +"</div>";
	$(document.body).append(div);
	var self = this;
	$("#"+idModal).draggable({ handle:"#"+idTitle});
	//$("#"+idTitle).on("mousedown",function(e){//拖拽改变位置
	//	self._dragMousedown(e);
	//});
//	$(document).on("mouseup","#"+self.id+",#"+self.id+" div",function(){//停止拖拽
//        $("#"+idTitle).css("cursor","default");
//        $(document).off("mousemove","#"+self.id+",#"+self.id+" div");
//        if(self.resizable){
//        	$(document).on("mousemove","#"+self.id+",#"+self.id+" div",function(e){
//        		self._resizeMousemove(e);
//        	});//改变大小;
//        }
//    });
//	if(self.resizable){
//		$(document).on("mousemove","#"+self.id+",#"+self.id+" div",function(e){
//			self._resizeMousemove(e);//改变大小
//		});
//		$("#modal").on("mousedown",function(e){
//			if(self.cursor=="default"){
//				return;
//			}
//			self.resizing = true;
//			if(e.preventDefault){
//	            e.preventDefault();
//	        }else{
//	            e.returnValue = false;
//	        }
//		});
//		$(document).on("mouseup",function(e){
//			self.resizing = false;
//			self.cursor = "default";
//	    	$("#body"+self.id).css("cursor",self.cursor);
//		});
//	}
	$("#"+idMin).on("click",function(){
		if(this.title == "最小化"){
			this.title = "最大化";
			this.style.background = "url(images/dialog_restore.png) #969495";
			$("#"+idMask).hide();
			$("#"+idTitle).css({background:"#969495",textIndent:"5px"});
	        $("#"+idModal).css({top:document.documentElement.clientHeight-30,left:0,width:200,height:30,overflow:"hidden"});
		}else{
			this.title = "最小化";
			this.style.background = "url(images/dialog_min.png) #969495";
			$("#"+idMask).show();
			$("#"+idTitle).css({background:"",textIndent:""});
			$("#"+idModal).css({top:self.top,left:self.left,width:self.width,height:self.height});
		}
	});
	$("#"+idClose).on("click",function(){
		parent.$('#tabsDivD').css('display','none');
		self.close();
	});
	$("#"+idClose).css("cursor","pointer");
	if(url == null){
		return;
	}
	$("#"+idContent).load(url,function(){
		if(self.initFun!=null){
			self.initFun();
			if(typeof(param.height)=="undefined"||!param.height){
				
			}
		}
	});
}

JYYModal.prototype = {
    url : "",
    initFun : null,
    backFun : null,
    title : "",
    width : 500,
    height : 400,
    left : 0,
    top : 0,
    id : 0,
    threshold:20,
	cursor : "default",
	resizable : false,
	resizing : false,
	x0 : 0,
	y0 : 0,
	offsetX0 : 0,
	offsetY0 : 0,
	_open : function(){
		
	},
	close : function(){
		$(document).off("mousemove","#"+this.id+",#"+this.id+" div");
		$("#"+this.id).remove();
	},
//	_dragMousedown : function(e){
//		var self = this;
//		$("#title"+this.id).css("cursor","move");//改变鼠标指针的形状 
//		self.x0 = e.pageX;
//		self.y0 = e.pageY;
//		self.offsetX0 = $("#title"+self.id).offset().left;
//		self.offsetY0 = $("#title"+self.id).offset().top;
//        var msg0 = "初始div位置：x:"+self.offsetX0+"，y:"+self.offsetY0+"<br>鼠标初始位置：x："+self.x0+"，y："+self.y0;
//        $("#msg000000").html(msg0);
//        $(document).off("mousemove","#"+self.id+",#"+self.id+" div").on("mousemove","#"+self.id+",#"+self.id+" div",function(ev){
//        	self._dragMousemove(ev);
//        });
//        if(e.preventDefault){
//            e.preventDefault();
//        }else{
//            e.returnValue = false;
//        }
//	},
//	_dragMousemove : function(ev){
//		var w = $(window).width();
//    	var h = $(window).height();
//        this.left = ev.pageX - this.x0 + this.offsetX0;
//        this.top = ev.pageY - this.y0 + this.offsetY0;
//        if(ev.pageX<=0||ev.pageY<=0||ev.pageX>=w||ev.pageY>=h){
//        	return;
//        }
//    	var msg1 = "<br>鼠标当前位置：x："+ev.pageX+"，y:"+ev.pageY+"<br>当前div位置：x:"+this.left+"，y:"+this.top;
//    	msg1 += "<br>移动位置：x1:"+(ev.pageX - this.x0)+"，y1:"+(ev.pageY - this.y0);
//    	$("#msg000000").html(msg1);
//        $("#modal"+this.id).animate({left:this.left+"px",top:this.top+"px"},1);
//	},
	_resizeMousemove : function resizeModal(e){
    	var x0 = e.pageX;
        var y0 = e.pageY;
        var offsetX0 = $("#body"+this.id).offset().left;
        var offsetY0 = $("#body"+this.id).offset().top;
    	var w = $("#body"+this.id).width();
    	var h = $("#body"+this.id).height();
    	if(!this.resizing){//鼠标未按下
            if(offsetX0-x0<this.threshold&&offsetX0-x0>-this.threshold){//左侧
            	if(y0>offsetY0&&y0<offsetY0+h-this.threshold){//上下之间，左边框
            		this.cursor = "w-resize";
            	}else if(y0>offsetY0+h-this.threshold&&y0<offsetY0+h+this.threshold){//左下角
            		this.cursor = "sw-resize";
            	}
            }else if(offsetX0+w-x0<this.threshold&&offsetX0+w-x0>-this.threshold){//右侧
            	if(y0>offsetY0&&y0<offsetY0+h-this.threshold){//上下之间，右边框
            		this.cursor = "e-resize";
            	}else if(y0>offsetY0+h-this.threshold&&y0<offsetY0+h+this.threshold){//右下角
            		this.cursor = "se-resize";
            	}
            }else if(x0-offsetX0>this.threshold&&offsetX0+w-x0>this.threshold){
            	if(y0>offsetY0+h-this.threshold&&y0<offsetY0+h+this.threshold){
            		this.cursor = "s-resize";
            	}
            }
            $("#body"+this.id).css("cursor",this.cursor);
    	}else{//鼠标按下
   			var wMax = $(window).width();
           	var hMax = $(window).height();
           	var msg2 = "鼠标按下："+this.cursor;
    		if(this.cursor == "e-resize"){//右
    			if(x0<wMax&&x0-offsetX0>this.threshold){
    				this.width = x0 - offsetX0;
    				$("#modal"+this.id).width(this.width);
    				$("#title"+this.id).width(this.width);
    				$("#body"+this.id).width(this.width);
    			}
    		}else if(this.cursor == "w-resize"){//左
    			if(x0>0&&offsetX0+w-x0>this.threshold){
    				msg2+="<br>(w:"+w+","+offsetX0+")，移动后：宽:"+(offsetX0+w-x0)+",左边距："+x0;
    				this.width = offsetX0+w-x0;
    				this.left = x0;
    				$("#modal"+this.id).width(this.width);
    				$("#title"+this.id).width(this.width);
    				$("#body"+this.id).width(this.width);
    				$("#modal"+this.id).css("left",this.left);
    			}
    		}else if(this.cursor == "s-resize"){//下
    			if(y0<hMax&&y0-offsetY0>this.threshold){
    				this.height = y0-offsetY0;
    				$("#body"+this.id).height(this.height);
    				this.height += $("#title"+this.id).heigth;
    				$("#modal"+this.id).height(this.height);
    			}
    		}else if(this.cursor == "sw-resize"){//左下角
    			if(x0>0&&offsetX0+w-x0>this.threshold){
    				this.width = offsetX0+w-x0;
    				this.left = x0;
    				$("#modal"+this.id).width(this.width);
    				$("#title"+this.id).width(this.width);
    				$("#body"+this.id).width(this.width);
    				$("#modal"+this.id).css("left",this.left);
    			}
    			if(y0<hMax&&y0-offsetY0>this.threshold){
    				this.height = y0-offsetY0;
    				$("#body"+this.id).height(this.height);
    				this.height += $("#title"+this.id).heigth;
    				$("#modal"+this.id).height(this.height);
    			}
    		}else if(this.cursor == "se-resize"){//右下角
    			if(x0<wMax&&x0-offsetX0>this.threshold){
    				this.width = x0 - offsetX0;
    				$("#modal"+this.id).width(this.width);
    				$("#title"+this.id).width(this.width);
    				$("#body"+this.id).width(this.width);
    			}
    			if(y0<hMax&&y0-offsetY0>this.threshold){
    				this.height = y0-offsetY0;
    				$("#body"+this.id).height(this.height);
    				this.height += $("#title"+this.id).heigth;
    				$("#modal"+this.id).height(this.height);
    			}
    		}
    		$("#msg000000").html(msg2);
    	}
    }
};

jyyModal.progress = function(url){
	$(document.body).load("modal/progress.html", function(){
	});
}

jyyModal.putModal = function(modal){
	jyyModal.mapModal[modal.id] = modal;
}